package com.kang.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.kang.dao.InventoryLossMapper;
import com.kang.dao.ItemDetailMapper;
import com.kang.pojo.InventoryLoss;
import com.kang.pojo.InventoryLossStatistics;
import com.kang.pojo.InventoryLoss_itemInformation;
import com.kang.pojo.ItemDetail;
import com.kang.service.InventoryLossService;
import com.vonechina.entity.page.ItemLossPage;
import com.vonechina.entity.page.LossListPage;
import com.vonechina.entity.page.LossStatisticsListPage;

/**
 * 库存报损--服务层接口实现类
 * @author wd
 *
 */
@Service
public class InventoryLossServiceImpl implements InventoryLossService {

	/**
	 * 库存报损--DAO层持久层
	 */
	@Resource
	private InventoryLossMapper inventoryLossMapper;
	/**
	 * 物品批次-DAO层持久层
	 */
	@Resource
	private ItemDetailMapper idm;
	
	/**
	 * 查询报损列表总条数
	 */
	@Override
	public int selectByWhereCount(LossListPage lossListPage) {
		return inventoryLossMapper.selectByWhereCount(lossListPage);
	}

	/**
	 * 查询报损列表
	 */
	@Override
	public List<InventoryLoss> selectByWhereList(LossListPage lossListPage) {
		return inventoryLossMapper.selectByWhereList(lossListPage);
	}
	/**
	 * 根据报损id查询报损详情物品列表
	 */
	@Override
	public List<InventoryLoss_itemInformation> listItemByInventorylossid(Integer inventorylossid) {
		return inventoryLossMapper.listItemByInventorylossid(inventorylossid);
	}

	/**
	 * 根据报损id查询报损信息
	 */
	@Override
	public InventoryLoss getInventoryLossById(Integer inventorylossid) {
		return inventoryLossMapper.getInventoryLossById(inventorylossid);
	}

	/**
	 * 根据库存报损id删除库存报损及相关报损物品
	 */
	@Override
	public boolean deleteInventoryLossById(int inventoryLossID) {
		int sql = inventoryLossMapper.deleteInventoryLossById(inventoryLossID);
		if(sql > 0){
			inventoryLossMapper.deleteInventoryLossToItemById(inventoryLossID);
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 添加报损物品时查询物品列表
	 */
	@Override
	public List<ItemDetail> listItemDetailForLoss(ItemLossPage page) {
		page.setRows(idm.countItemNotInForLoss(page));
		return idm.listItemNotInForLoss(page);
	}

	/**
	 * 修改报损单信息
	 */
	@Override
	public int updateInventoryLoss(InventoryLoss inventoryLoss) {
		return inventoryLossMapper.updateInventoryLoss(inventoryLoss);
	}

	/**
	 * 删除报损单的中不属于当前集合的物品id的物品
	 */
	@Override
	public int deleteItemNotInLossSingle(Integer inventorylossid,Integer[] itemIds) {
		return inventoryLossMapper.deleteItemNotInLossSingle(inventorylossid,itemIds);
	}

	/**
	 * 保存报损单中的物品
	 */
	@Override
	public int saveLossItemList(Integer inventorylossid, Integer[] itemIds,
			Integer[] number, Integer[] itemInformationIDs, String[] lossReasonS,Integer warehouseid) {
		if(itemIds!=null){
			for (int i = 0; i < itemIds.length; i++) {
				InventoryLoss_itemInformation iiInfo = inventoryLossMapper.selectLossItemInfoByItemid(inventorylossid,itemIds[i]);
				if(iiInfo==null){
					iiInfo = new InventoryLoss_itemInformation();
					iiInfo.setInventoryLossID(inventorylossid);
					iiInfo.setWarehouseID(warehouseid);
					iiInfo.setItemInformationID(itemInformationIDs[i]);
					iiInfo.setLossNumber(number[i]);
					iiInfo.setLossReason(lossReasonS[i]);
					iiInfo.setSourceID(itemIds[i]);
					inventoryLossMapper.insertNewItemToLoss(iiInfo);
				}else{
					iiInfo.setLossNumber(number[i]);
					iiInfo.setLossReason(lossReasonS[i]);
					inventoryLossMapper.updateItemNumberToLoss(iiInfo);
				}
			}
			return itemIds.length;
		}else{
			return 0;
		}
		
	}

	/**
	 * 提交报损单
	 */
	@Override
	public int saveAndSubmitInventoryLoss(Integer userID,Integer inventorylossid) {
		return inventoryLossMapper.saveAndSubmitInventoryLoss(userID,inventorylossid);
	}

	/**
	 * 生成报损单号
	 */
	@Override
	public String selectMaxLossNumber() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String text = sdf.format(new Date()) + "0001";
		Long no = Long.parseLong(text);
		String max = inventoryLossMapper.selectMaxLossNumber();
		Long l = 0l;
		if (max != null) {
			l = Long.parseLong(max.replaceAll("[a-zA-Z]", ""));
		}
		if (l >= no) {
			no = l + 1;
		}
		return "BS" + no;
	}
	
	/**
	 * 新增报损单信息
	 */
	@Override
	public int addNewInventoryLoss(InventoryLoss inventoryLoss) {
		return inventoryLossMapper.addNewInventoryLoss(inventoryLoss);
	}

	/**
	 * 根据报损单号查询报损id
	 */
	@Override
	public Integer getLossIdByLossNumber(String lossnumber) {
		return inventoryLossMapper.getLossIdByLossNumber(lossnumber);
	}

	/**
	 * 报损提交后修改实际库存数量
	 */
	@Override
	public int submitUpdateLossAfterNumberForStock(Integer[] itemIds,Integer[] number) {
		for (int i = 0; i < itemIds.length; i++) {
			idm.updateBatchInventoryNumberForLoss(itemIds[i],number[i]);
		}
		return itemIds.length;
	}

	/**
	 * 库存报损统计
	 */
	@Override
	public List<InventoryLossStatistics> selectInventoryLossStatisticsList(LossStatisticsListPage lossStatisticsListPage) {
		lossStatisticsListPage.setRows(inventoryLossMapper.selectInventoryLossStatisticscount(lossStatisticsListPage));
		return inventoryLossMapper.selectInventoryLossStatisticsList(lossStatisticsListPage);
	}

}
